package com.dpj.Service;

import com.alibaba.fastjson.JSONObject;
import com.dpj.mapper.BrandMapper2;
import com.dpj.pojo.Brand;
import com.dpj.utils.SqlSessionFactoryUtils1;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class BrandService {

    SqlSessionFactory sessionFactory = SqlSessionFactoryUtils1.getSessionFactory();
    public List<Brand> selectAll() throws IOException {



        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper2 mapper = sqlSession.getMapper(BrandMapper2.class);
        List<Brand> lists=mapper.selectAll();
        sqlSession.close();
        return lists;
    }

    public int addOneBrand(String s) {
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper2 mapper = sqlSession.getMapper(BrandMapper2.class);
        Brand brand = JSONObject.parseObject(s, Brand.class);
       int i= mapper.addOneBrnad(brand);
       return i;
    }



    public int addOrUpdate(Brand brand) {
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper2 mapper = sqlSession.getMapper(BrandMapper2.class);
        if(brand.getId()!=null){
            mapper.updateById(brand);
            sqlSession.close();
            return 2;
        }else{
            mapper.addOneBrnad(brand);

            sqlSession.close();
            return 1;
        }
    }

    public void deleteById(Integer integer) {
        int i=integer;
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper2 mapper = sqlSession.getMapper(BrandMapper2.class);
        mapper.deleteById(i);
        sqlSession.close();
    }

    public void deleteByIds(List list) {
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper2 mapper = sqlSession.getMapper(BrandMapper2.class);
        mapper.deleteByIds(list);
        sqlSession.close();
    }

    //使用mybatis插件进行查询
    public PageInfo<Brand> selectByPage(int pageNum,int pageSize){
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper2 mapper = sqlSession.getMapper(BrandMapper2.class);
        PageHelper.startPage(pageNum,pageSize);
        List<Brand> brands = mapper.selectAll();
        PageInfo<Brand> pi=new PageInfo<>(brands);
        sqlSession.close();
        return pi;
    }
    //使用mybatis插件进行条件查询
    public PageInfo<Brand> selectByPageAndCondition(Integer pageNum, Integer pageSize, Brand brand) {
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper2 mapper = sqlSession.getMapper(BrandMapper2.class);
        PageHelper.startPage(pageNum,pageSize);
        List<Brand> brands=mapper.selectByCondition(brand);
        System.out.println(brands);
        PageInfo<Brand> pi=new PageInfo<>(brands);
        return pi;

    }
}
